#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include "Component\Physics\Transform.h"
#include "Component\Renderer\Base\RendererControl.h"
#include "Component\Renderer\SpriteRenderer.h"
#include "Component\Renderer\Square.h"
#include "Component\UI\TransformUI.h"
#include "Core\Cardinal.h"
#include "Core\Input.h"
#include "Core\ResourceControl.h"
#include "Glad/GLFW/glfw3.h"
#include "Glad/glad/glad.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
  glViewport(0, 0, width, height);
}

void processInput(GLFWwindow* window) {
  if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    glfwSetWindowShouldClose(window, true);
}
int main() {
  srand((unsigned long long)time(NULL));
  glfwInit();

  // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  // glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
  // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

  GLFWwindow* window =
      glfwCreateWindow(800, 600, "SakuraAgeLancher", NULL, NULL);
  if (window == NULL) {
    std::cout << "Failed to create GLFW window" << std::endl;
    glfwTerminate();
    return -1;
  }

  glfwMakeContextCurrent(window);
  if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
    std::cout << "Failed to initialize GLAD" << std::endl;
    return -1;
  }

  glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  glfwSetMouseButtonCallback(window, SakuraAge::Input::_mouseButtonCallback);
  glfwSetCursorPosCallback(window, SakuraAge::Input::_mousePositionCallback);

  SakuraAge::Input::_SetWindow(window);

  using namespace SakuraAge;
  // ResourceControl::Init();
  RendererControl::WSize = new Vector2(800, 600);
  RendererControl::BackGroundColor->r = 0;
  /*Item* i = ItemFactor::CreateItem();
  SpriteRenderer* s = Component::Add<SpriteRenderer>(i);
  s->texturePath = "Sprite/Picture.bmp";
  s->Awake();
  i->transform->position = Vector2(400,300);
  i->transform->scale = Vector2(348/2,102/2);
  i->transform->rotation = 0;*/
  RendererControl::BackGroundColor->r = 0;
  if (Cardinal::scenes.empty()) {
    Cardinal::scenes.push_back(new Scene(false));
  }
  while (!glfwWindowShouldClose(window)) {
    processInput(window);
#define c (*RendererControl::BackGroundColor)
    glClearColor(c.r, c.g, c.b, c.a);
    glClear(GL_COLOR_BUFFER_BIT);
    Cardinal::Update();

    glfwSwapBuffers(window);
    glfwPollEvents();
  }
  glfwTerminate();
  ResourceControl::Exit();
  return 0;
}